package com.itheima.dao.impl;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.awt.*;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private QueryRunner queryRunner;

    @Override
    public void save() throws SQLException {
        String sql = "  insert into tal_account values (?,?,?)";

        Object[] params = {null,"张柏芝",10000};

        int count = queryRunner.update(sql, params);

        System.out.println(count);
    }

    @Override
    public void select() throws SQLException {

        String sql = "select * from tal_account";

        List<User> query = queryRunner.query(sql, new BeanListHandler<User>(User.class));

        for (User user : query) {
            System.out.println(user);
        }
    }

    @Override
    public void del(int i) throws SQLException {
        String sql = "delete from tal_account where id = ?";

        Object[] params = {i};

        int count = queryRunner.update(sql, params);

        System.out.println(count);
    }

    @Override
    public void update() throws SQLException {
        String sql = "update tal_account set username = ? where id = ?";

        Object[] params = {"雷无桀",1};

        int count = queryRunner.update(sql, params);

        System.out.println(count);
    }
}
